import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import { getDepts, updateDepts, insertDepts, deleteDepts, batchSaveDepts,getDeptRooms } from '@/api/depts'
import store from '@/store'

export interface IDeptState {

}

@Module({ dynamic: true, store, name: 'dept' })
class Depts extends VuexModule implements IDeptState {

    @Action
    public async getDepts() {
        return new Promise((r: any, j: any) => {
            getDepts({}).then((value: any) => {
                console.error("Depts:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateDepts(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDepts(obj).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j({})
            })
        })
    }

    @Action
    public async insertDepts(obj: any) {
        return new Promise((r: any, j: any) => {
            insertDepts(obj).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j({})
            })
        })
    }

    @Action
    public async deleteDepts(deptId: string) {
        return new Promise((r: any, j: any) => {
            deleteDepts({ deptId: deptId }).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

    @Action
    public async getDeptRooms(deptId: string) {
        return new Promise((r: any, j: any) => {
            getDeptRooms({ deptId: deptId }).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

    @Action
    public async batchSaveDepts(depts: any) {
        console.error(depts)

        return new Promise((r: any, j: any) => {
            batchSaveDepts({ depts: depts }).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

}

export const DeptsModule = getModule(Depts)
